<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
class cateController extends Controller
{
	//显示商品分类
    public function index()
    {
    	$list = DB::table('category')->get();
    	//dd($list);
    	return view('goods.category.index',['list'=>$list]);
    }

    //添加商品分类
    public function create()
    {
    	return view('goods.category.add');
    }

    public function store(Request $request)
    {
    	// dd($request);
    	$data = $request->except('_token');
    	$id = DB::table('category')->insertGetId($data);
    	// dd($id);
    	if($id>0){
    		return redirect('/admin/cate')->with('msg','添加成功');
    	}
    }
    //删除
    public function destroy($id)
    {
    	$id = DB::table('category')->where('id',$id)->delete();
    	if($id>0){
    		return redirect('/admin/cate');
    	}
    }

    //修改
    public function edit($id)
    {
    	$cate = DB::table('category')->where('id',$id)->first();
    	// dd($cate);
    	return view('goods.category.edit',['cate'=>$cate]);
    }

    public function update($id, Request $request)
    {
    	$data = $request->only('name');
    	$row = DB::table('category')->where('id',$id)->update($data);
    	if($row>0){
    		return redirect('admin/cate')->with('msg','修改成功');
    	}
    }


    //添加子类
    public function createSon($id)
    {
    	$list = DB::table('category')->where('id',$id)->first();

    	// dd($list);
    	return view('goods.category.addSon',['list'=>$list]);
    }
    public function storeSon(Request $request)
    {
    	// dd($request->all());
    	$data = $request->except('_token');
        $id = DB::table('category')->insertGetId($data);
        if($id>0){
            return redirect('admin/cate')->with('msg','添加子类别成功');
        }
    }
}
